package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import util.PropertyLoader;

import java.util.concurrent.TimeUnit;

public class HomePage extends BasePage
{

    private static final String URL = PropertyLoader.loadProperty("site.url");

    private static final By SIGNIN = By.name("signin");
    private static final By FIELD_LOGIN = By.name("login");
    private static final By FIELD_PASSWORD = By.name("password");
    private static final By SUBMIT_BLUE_BUTTON = By.tagName("button");

    private static final By SIGNOUT = By.name("signout");

    private static final By SEARCH_FIELD = By.className("header-search-input-text");
    private static final By SUBMIT_SEARCH_BUTTON = By.className("btn-link-i");

    public HomePage(WebDriver driver)
    {
        super(driver);
    }

    public void open()
    {
        driver.get(URL);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    // ****************** Authorisation ********************

    public void logIn(String email,String password)
    {
        driver.findElement(SIGNIN).click();

        driver.findElement(FIELD_LOGIN).clear();
        driver.findElement(FIELD_LOGIN).sendKeys(email);

        driver.findElement(FIELD_PASSWORD).clear();
        driver.findElement(FIELD_PASSWORD).sendKeys(password);

        driver.findElement(SUBMIT_BLUE_BUTTON).click();
    }
    public boolean isLoggedIn()
    {
        return driver.findElements(SIGNOUT).size()>0;

    }
    public void logOut()
    {
       driver.findElement(SIGNOUT).click();
    }

    public void searchProduct(String product)
    {
        driver.findElement(SEARCH_FIELD).clear();
        driver.findElement(SEARCH_FIELD).sendKeys(product);
        driver.findElement(SUBMIT_SEARCH_BUTTON).click();
    }
    public boolean productOnPage(String product)
    {
        return (driver.findElements(By.xpath("//a[contains(text(),'"+product+"')]")).size()>0);
    }
}


